//package com.lagou.edu.filter;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.apache.dubbo.config.annotation.Reference;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpCookie;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//import service.UserService;
//
//
///**
// * @ClassName LoginFilter
// * @Description TODO
// * @Auth ying.xiao
// * @Date 2021/4/21
// * @Version 1.0.0
// */
//@Component
//@Slf4j
//public class LoginFilter implements GlobalFilter, Ordered {
//
//    @Reference
//    private UserService userService;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        //过滤接口
//        String url = request.getPath().value();
//        if(url.startsWith("/api/user/login")||
//                url.startsWith("/api/user/register")||
//                url.startsWith("/api/code/create")){
//            return chain.filter(exchange);
//        }
//        //获取token值
//        HttpCookie token1 = request.getCookies().getFirst("token");
//        //验证token是否非法
//        if(null == token1 || (StringUtils.isBlank(token1.getValue()))
//        || StringUtils.isBlank(userService.info(token1.getValue()))){
//            response.setStatusCode(HttpStatus.SEE_OTHER);
//            response.getHeaders().set("Location", "http://localhost:9006/static/login.html");
//            return exchange.getResponse().setComplete();
//        }
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
